﻿
Imports System.Net.Sockets

Imports System.Net


Imports System.Text

Imports System.Threading

Imports System.Runtime.InteropServices
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Windows.Forms.Timer

Public Class Form1
    Dim s As Socket = Nothing
    Dim t As Thread
    Dim MyIP As String
    Public Sub WaitData()
        Dim temp As String
        s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) '''使用TCP协议
        Dim localEndPoint As New IPEndPoint(IPAddress.Parse(MyIP), 1025)  '''指定IP和Port
        s.Bind(localEndPoint)
        s.Listen(1000)     '''侦听，最多接受100个连接
        While (True)
            Dim bytes(2048) As Byte   '''用来存储接收到的字节
            Dim ss As Socket = s.Accept()  '''若接收到,则创建一个新的Socket与之连接
            ss.Receive(bytes)    '''接收数据，若用ss.send(Byte()),则发送数据
            temp = "[我来自服务器]" + vbCrLf + Encoding.Unicode.GetString(bytes)
            Console.WriteLine(temp)
            Try
                Me.Invoke(New Action(Sub() 收到内容.Items.Add(temp)))
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
            'RichTextBox1.Text = RichTextBox1.Text + vbCrLf + Encoding.Unicode.GetString(bytes)
            '' ListBox1.Items.Insert(0, Encoding.Unicode.GetString(bytes)) '''将其插入到列表框的第一项之前
            '''若使用Encoding.ASCII.GetString(bytes),则接收到的中文字符不能正常显示
        End While
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        RichTextBox2.Text = ""
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        '获取第二个文本的内容，就是text
        Dim chatContent = ""
        Dim currentTime As Date
        Dim addressIP = ""
        currentTime = DateTime.Now
        chatContent = RichTextBox2.Text
        addressIP = TextBox1.Text
        Try
            Dim bytes(1024) As Byte
            Dim s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            Dim localEndPoint As New IPEndPoint(IPAddress.Parse(addressIP), 1025)
            s.Connect(localEndPoint)
            s.Send(Encoding.Unicode.GetBytes(RichTextBox2.Text))
            s.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally
            MsgBox("处理完毕")
        End Try
        RichTextBox1.Text = RichTextBox1.Text + vbCrLf + currentTime + vbCrLf + chatContent
        RichTextBox2.Text = ""
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        t = New Thread(AddressOf WaitData)  '''建立新的线程
        t.Start()     '''启动线程
        Button4.Enabled = False   '''按钮不可用，避免另启线程
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim HostName As String
        Dim HostIP As IPHostEntry
        Dim IPAddress As IPAddress
        HostName = Dns.GetHostName
        HostIP = Dns.GetHostByName(Dns.GetHostName())
        IPAddress = New IPAddress(HostIP.AddressList(2).Address)
        MyIP = IPAddress.ToString
        MsgBox(MyIP)
        'MyIP = "127.0.0.1"
        'MsgBox(MyIP)
        Timer1.Interval = 20 ' 毫秒
        Timer1.Start()
    End Sub

    <DllImport("User32.dll", SetLastError:=True)>
    Public Shared Function PrintWindow(ByVal hwnd As IntPtr, ByVal hdc As IntPtr, ByVal nFlags As UInteger) As Integer
    End Function

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim bitmap01 As Bitmap
        Dim g01 As Graphics

        Try
            bitmap01 = New Bitmap(1872, 909)  '要截图的目标窗体尺寸
            g01 = Graphics.FromImage(bitmap01)
            PrintWindow(Me.Handle, g01.GetHdc, 0)  'hwd_01  为目标窗体的hwd句柄
            g01.ReleaseHdc()
            PictureBox1.Image = bitmap01
        Catch ex As Exception

        End Try
        'MessageBox.Show("按钮被点击了！")
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Button4.PerformClick()
    End Sub
End Class

